package com.csxh.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.csxh.web.bean.QueryCondition;
import com.csxh.web.service.QueryConditionService;
import com.csxh.web.util.StringUtil;
import com.csxh.web.util.WebUtil;

/**
 * Servlet implementation class QueryConditionServlet
 */
@WebServlet("/sqlCondition.servlet")
public class QueryConditionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QueryConditionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*
		 * <jsp:include page="sqlCondition.jsp"> <jsp:param name="filter"
		 * value="true" /> <jsp:param name="beanClassName"
		 * value="com.csxh.web.bookstore.bean.Book" /> <jsp:param
		 * name="includeFields" value="id"/> <jsp:param name="excludeFields"
		 * value="password"/> </jsp:include>
		 */
		// 获取查询参数
		String beanClassName = WebUtil.strParam(request, WebUtil.BEAN_CLASS_NAME, null);
		Class<?> clazz;
		try {
			clazz = Class.forName(beanClassName);
		} catch (ClassNotFoundException e) {
			return;
		}

		String s = WebUtil.strParam(request, WebUtil.INCLUDE_FIELDS, "");
		String[] includeFields = null;
		if (!StringUtil.isEmpty(s)) {
			includeFields = s.split("[\\s,]");
		}

		s = WebUtil.strParam(request, WebUtil.EXCLUDE_FIELDS, "");
		String[] excludeFields = null;
		if (!StringUtil.isEmpty(s)) {
			excludeFields = s.split("[\\s,]");
		}
		// 获取查询条件
		QueryConditionService service = new QueryConditionService();
		List<QueryCondition> list = service.build(clazz, includeFields, excludeFields);

		request.setAttribute(WebUtil.QUERY_CONDITION_LIST, list);

		// 构建字段查询表达式映射

		Map<String, List<String>> field2ExpressionListMap = new HashMap<String, List<String>>();
		for (QueryCondition queryCondition : list) {

			String key = queryCondition.getFieldName();
			List<String> value = queryCondition.getExpressionList();

			field2ExpressionListMap.put(key, value);

		}

		// 将java对象转成前台jQuery使用的json格式
		String fieldQueryExpression = JSON.toJSONString(field2ExpressionListMap);
		// 保存到上下文中
		request.setAttribute(WebUtil.QUERY_EXPRESSION, fieldQueryExpression);

		// 为前台编写两级联动js
		StringBuffer sb = new StringBuffer();
		sb.append("");

		response.getWriter().append("<script type='text/javascript'>");
		request.getRequestDispatcher("select.js").include(request, response);
		response.getWriter().append("</script>");
		
	}

}
